﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RayDen.RayEngine.Core;
using RayDen.RayEngine.Core.Devices;
using RayDen.RayEngine.Core.Interface;
using RayDen.RayEngine.Core.Threads;
using RayDen.RayEngine.Managers;
using RayDen.RayEngine.Samplers;

namespace RayDen.RayEngine.Engines.MCMC
{

    public class PssMltRenderThread : DeviceRenderThread<PssMltPathBuffer>
    {
        public PssMltRenderThread(int index,
                                  IIntersectionEngine device,
                                  RayEngineScene scn,
                                  bool lowLatency,
                                  IImageFilm pixelDevice,
                                  SamplingContext sc, IFrameManager frameManager)
            : base(index, device, scn, lowLatency, pixelDevice, sc, frameManager) { }

        protected override PssMltPathBuffer CreatePathBuffer(int maxPath, RayEngineScene scene, IImageFilm pixelDevice, SamplingContext sc)
        {
            var pt =  base.CreatePathBuffer(maxPath, scene, pixelDevice, sc);
            pt.controller = new PssMltEngineController();
            pt.controller.Init(pixelDevice.Width, pixelDevice.Height);
            return pt;
        }
    }


    //[RenderingEngine(EngineType = RenderingEngines.PssMlt)]
    public class PssMltEngine : BaseRenderingEngine
    {
        public PssMltEngine(RayEngineScene scene)
            : base(scene)
        {
        }

        public override RenderThread CreateRenderThread(int threadIndex, IntersectionEngine intersectionDevice, RayEngineScene scene, 
                                                        IImageFilm film, IFrameManager manager, SamplingContext samplingContext)
        {
            return new PssMltRenderThread(threadIndex, intersectionDevice, scene, LowLatencySampling, film, samplingContext, manager);

        }

        public override ISampler CreateSampler(int width, int height, int maxPathDepth, int threadIndex, int screenStartLine, int windowHeight)
        {
            return new PSSMLTSampler() { screenWidth = width, screenHeight = height, lowLatency = true, screenStartLine = screenStartLine + 1, tIdx = threadIndex };
            /*
            return SamplerFactory.Instance.CreateSampler(width,
                                                       height,
                                                       maxPathDepth,
                                                       screenStartLine,
                                                       LowLatencySampling,
                                                       threadIndex, windowHeight);*/
        }

        public override IImageFilm CreateFilm(int width, int height)
        {
            return new ImageFilm<PrimarySampleSpacePathSampler>(width, height);
        }
    }
}
